/**
 * 
 */
package com.laivi.basic.app.concurrent;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author janlu
 *
 */
public class HorseRace {
	static final int FINISH_LINE=75;
	private List<Horse> horses=new ArrayList<Horse>();
	private ExecutorService exec=Executors.newCachedThreadPool();
	private CyclicBarrier barrier;
	
	public HorseRace(int nHorse,final int pause){
		barrier=new CyclicBarrier(nHorse,new Runnable(){
			@Override
			public void run() {
				StringBuilder sb=new StringBuilder();
				for(int i=0;i<FINISH_LINE;i++){
					sb.append("=");
				}
				System.out.println(sb);
				for(Horse horse:horses){
					System.out.println(horse.tracks());
				}
				for(Horse horse:horses){
					if(horse.getStrides()>=FINISH_LINE){
						System.out.println(horse+" won!");
						exec.shutdownNow();
						return ;
					}
				}
				try{
					TimeUnit.MILLISECONDS.sleep(pause);
				}catch(InterruptedException e){
					System.out.println("barrier-action sleep interrupted");
				}
				
			}
			
		});
		for(int i=0;i<nHorse;i++){
			Horse horse=new Horse(barrier);
			horses.add(horse);
			exec.execute(horse);
		}
	}
	
	public static void main(String[] args){
		int nHorses=7;
		int pause=200;
		new HorseRace(nHorses,pause);
	}
}
